package com.hitalkie.teacher.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.hitalkie.teacher.R;
import com.hitalkie.teacher.adapter.StudentRvAdapter;
import com.hitalkie.teacher.model.Lesson;
import com.hitalkie.teacher.model.User;
import com.hitalkie.teacher.widget.pageview.OnLastItemVisibleListener;
import com.hitalkie.teacher.widget.pageview.PageRecyclerView;

/**
 * 只有当前用户是老师才能进入这个页面
 * <p>
 * Date 09/12/2016
 *
 * @author charliema
 */

public class StudentListOfLessonActivity extends BaseActivity {

    public static final String EXTRA_LESSON = "com.hitalkie.talkie.extra.LESSON";

    private Lesson mLesson;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initView();
        initData();

    }

    private void initView() {
        setContentView(R.layout.view_student_list_of_lesson);
        initTitleBar();
        initSrl();
        initRv();
    }

    private void initTitleBar() {
        findViewById(R.id.viewTitleBarLeft).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().finish();
            }
        });
    }

    private void initSrl() {
        ((SwipeRefreshLayout) findViewById(R.id.srl)).setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
//                updateData(true);
                ((SwipeRefreshLayout) findViewById(R.id.srl)).setRefreshing(false);
            }
        });
    }

    private void initRv() {
        PageRecyclerView rv = (PageRecyclerView) findViewById(R.id.rv);
        rv.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
        rv.setAdapter(new StudentRvAdapter());
        rv.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {
            @Override
            public void onLastItemVisible() {
//                updateData(false);
            }
        });
        ((StudentRvAdapter) rv.getAdapter()).setOnItemClickListener(new StudentRvAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(User data) {

            }

            @Override
            public void onEvaluation(User data) {
                Intent i = new Intent();
                i.setClass(getActivity(), EvaluationActivity.class);
                i.putExtra(EvaluationActivity.EXTRA_LESSON, mLesson);
                i.putExtra(EvaluationActivity.EXTRA_TEACHER, mLesson.teachers.get(0));
                i.putExtra(EvaluationActivity.EXTRA_VIP, data);
                startActivity(i);
            }
        });
    }

    private void initData() {
        mLesson = (Lesson) getIntent().getSerializableExtra(EXTRA_LESSON);
        updateRv();
    }

    private void updateRv() {
        ((StudentRvAdapter) ((RecyclerView) findViewById(R.id.rv)).getAdapter()).setData(mLesson.vips);
    }


}
